home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / Pager / Sliding.php < prev   
Encoding:
PHP Script  |  2005-10-16  |  11.3 KB  |  307 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Pager_Sliding class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. The name of the author may not be used to endorse or promote products
  17.  *    derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  20.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  23.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @category   HTML
  31.  * @package    Pager
  32.  * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
  33.  * @copyright  2003-2005 Lorenzo Alberton
  34.  * @license    http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  35.  * @version    CVS: $Id: Sliding.php,v 1.8 2005/07/22 16:28:11 quipo Exp $
  36.  * @link       http://pear.php.net/package/Pager
  37.  */
  38.  
  39. /**
  40.  * require PEAR::Pager_Common base class
  41.  */
  42. require_once 'Pager/Common.php';
  43.  
  44. /**
  45.  * Pager_Sliding - Generic data paging class  ("sliding window" style)
  46.  * Usage examples can be found in the PEAR manual
  47.  *
  48.  * @category   HTML
  49.  * @package    Pager
  50.  * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
  51.  * @copyright  2003-2005 Lorenzo Alberton
  52.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  53.  * @link       http://pear.php.net/package/Pager
  54.  */
  55. class Pager_Sliding extends Pager_Common
  56. {
  57.     // {{{ Pager_Sliding()
  58.  
  59.     /**
  60.      * Constructor
  61.      *
  62.      * @param mixed $options    An associative array of option names
  63.      *                          and their values
  64.      * @access public
  65.      */
  66.     function Pager_Sliding($options = array())
  67.     {
  68.         //set default Pager_Sliding options
  69.         $this->_delta                 = 2;
  70.         $this->_prevImg               = '«';
  71.         $this->_nextImg               = '»';
  72.         $this->_separator             = '|';
  73.         $this->_spacesBeforeSeparator = 3;
  74.         $this->_spacesAfterSeparator  = 3;
  75.         $this->_curPageSpanPre        = '<b><u>';
  76.         $this->_curPageSpanPost       = '</u></b>';
  77.  
  78.         //set custom options
  79.         $err = $this->_setOptions($options);
  80.         if ($err !== PAGER_OK) {
  81.             return $this->raiseError($this->errorMessage($err), $err);
  82.         }
  83.         $this->_generatePageData();
  84.         $this->_setFirstLastText();
  85.  
  86.         if ($this->_totalPages > (2 * $this->_delta + 1)) {
  87.             $this->links .= $this->_printFirstPage();
  88.         }
  89.  
  90.         $this->links .= $this->_getBackLink();
  91.         $this->links .= $this->_getPageLinks();
  92.         $this->links .= $this->_getNextLink();
  93.  
  94.         $this->linkTags .= $this->_getFirstLinkTag();
  95.         $this->linkTags .= $this->_getPrevLinkTag();
  96.         $this->linkTags .= $this->_getNextLinkTag();
  97.         $this->linkTags .= $this->_getLastLinkTag();
  98.  
  99.         if ($this->_totalPages > (2 * $this->_delta + 1)) {
  100.             $this->links .= $this->_printLastPage();
  101.         }
  102.     }
  103.  
  104.     // }}}
  105.     // {{{ getPageIdByOffset()
  106.  
  107.     /**
  108.      * "Overload" PEAR::Pager method. VOID. Not needed here...
  109.      * @param integer $index Offset to get pageID for
  110.      * @deprecated
  111.      * @access public
  112.      */
  113.     function getPageIdByOffset($index=null) { }
  114.  
  115.     // }}}
  116.     // {{{ getPageRangeByPageId()
  117.  
  118.     /**
  119.      * Given a PageId, it returns the limits of the range of pages displayed.
  120.      * While getOffsetByPageId() returns the offset of the data within the
  121.      * current page, this method returns the offsets of the page numbers interval.
  122.      * E.g., if you have pageId=5 and delta=2, it will return (3, 7).
  123.      * PageID of 9 would give you (4, 8).
  124.      * If the method is called without parameter, pageID is set to currentPage#.
  125.      *
  126.      * @param integer PageID to get offsets for
  127.      * @return array  First and last offsets
  128.      * @access public
  129.      */
  130.     function getPageRangeByPageId($pageid = null)
  131.     {
  132.         $pageid = isset($pageid) ? (int)$pageid : $this->_currentPage;
  133.         if (!isset($this->_pageData)) {
  134.             $this->_generatePageData();
  135.         }
  136.         if (isset($this->_pageData[$pageid]) || is_null($this->_itemData)) {
  137.             if ($this->_expanded) {
  138.                 $min_surplus = ($pageid <= $this->_delta) ? ($this->_delta - $pageid + 1) : 0;
  139.                 $max_surplus = ($pageid >= ($this->_totalPages - $this->_delta)) ?
  140.                                 ($pageid - ($this->_totalPages - $this->_delta)) : 0;
  141.             } else {
  142.                 $min_surplus = $max_surplus = 0;
  143.             }
  144.             return array(
  145.                 max($pageid - $this->_delta - $max_surplus, 1),
  146.                 min($pageid + $this->_delta + $min_surplus, $this->_totalPages)
  147.             );
  148.         }
  149.         return array(0, 0);
  150.     }
  151.  
  152.     // }}}
  153.     // {{{ getLinks()
  154.  
  155.     /**
  156.      * Returns back/next/first/last and page links,
  157.      * both as ordered and associative array.
  158.      *
  159.      * @param integer $pageID Optional pageID. If specified, links
  160.      *                for that page are provided instead of current one.
  161.      * @return array back/pages/next/first/last/all links
  162.      * @access public
  163.      */
  164.     function getLinks($pageID = null)
  165.     {
  166.         if ($pageID != null) {
  167.             $_sav = $this->_currentPage;
  168.             $this->_currentPage = $pageID;
  169.  
  170.             $this->links = '';
  171.             if ($this->_totalPages > (2 * $this->_delta + 1)) {
  172.                 $this->links .= $this->_printFirstPage();
  173.             }
  174.             $this->links .= $this->_getBackLink();
  175.             $this->links .= $this->_getPageLinks();
  176.             $this->links .= $this->_getNextLink();
  177.             if ($this->_totalPages > (2 * $this->_delta + 1)) {
  178.                 $this->links .= $this->_printLastPage();
  179.             }
  180.         }
  181.  
  182.         $back  = str_replace(' ', '', $this->_getBackLink());
  183.         $next  = str_replace(' ', '', $this->_getNextLink());
  184.         $pages = $this->_getPageLinks();
  185.         $first = $this->_printFirstPage();
  186.         $last  = $this->_printLastPage();
  187.         $all   = $this->links;
  188.         $linkTags = $this->linkTags;
  189.  
  190.         if ($pageID != null) {
  191.             $this->_currentPage = $_sav;
  192.         }
  193.  
  194.         return array(
  195.             $back,
  196.             $pages,
  197.             trim($next),
  198.             $first,
  199.             $last,
  200.             $all,
  201.             $linkTags,
  202.             'back'  => $back,
  203.             'pages' => $pages,
  204.             'next'  => $next,
  205.             'first' => $first,
  206.             'last'  => $last,
  207.             'all'   => $all,
  208.             'linktags' => $linkTags
  209.         );
  210.     }
  211.  
  212.     // }}}
  213.     // {{{ _getPageLinks()
  214.  
  215.     /**
  216.      * Returns pages link
  217.      *
  218.      * @return string Links
  219.      * @access private
  220.      */
  221.     function _getPageLinks($url = '')
  222.     {
  223.         //legacy setting... the preferred way to set an option now
  224.         //is adding it to the constuctor
  225.         if (!empty($url)) {
  226.             $this->_path = $url;
  227.         }
  228.         
  229.         //If there's only one page, don't display links
  230.         if ($this->_clearIfVoid && ($this->_totalPages < 2)) {
  231.             return '';
  232.         }
  233.  
  234.         $links = '';
  235.         if ($this->_totalPages > (2 * $this->_delta + 1)) {
  236.             if ($this->_expanded) {
  237.                 if (($this->_totalPages - $this->_delta) <= $this->_currentPage) {
  238.                     $expansion_before = $this->_currentPage - ($this->_totalPages - $this->_delta);
  239.                 } else {
  240.                     $expansion_before = 0;
  241.                 }
  242.                 for ($i = $this->_currentPage - $this->_delta - $expansion_before; $expansion_before; $expansion_before--, $i++) {
  243.                     $print_separator_flag = ($i != $this->_currentPage + $this->_delta); // && ($i != $this->_totalPages - 1)
  244.                     
  245.                     $this->range[$i] = false;
  246.                     $this->_linkData[$this->_urlVar] = $i;
  247.                     $links .= $this->_renderLink($this->_altPage.' '.$i, $i)
  248.                            . $this->_spacesBefore
  249.                            . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  250.                 }
  251.             }
  252.  
  253.             $expansion_after = 0;
  254.             for ($i = $this->_currentPage - $this->_delta; ($i <= $this->_currentPage + $this->_delta) && ($i <= $this->_totalPages); $i++) {
  255.                 if ($i < 1) {
  256.                     ++$expansion_after;
  257.                     continue;
  258.                 }
  259.  
  260.                 // check when to print separator
  261.                 $print_separator_flag = (($i != $this->_currentPage + $this->_delta) && ($i != $this->_totalPages));
  262.  
  263.                 if ($i == $this->_currentPage) {
  264.                     $this->range[$i] = true;
  265.                     $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
  266.                 } else {
  267.                     $this->range[$i] = false;
  268.                     $this->_linkData[$this->_urlVar] = $i;
  269.                     $links .= $this->_renderLink($this->_altPage.' '.$i, $i);
  270.                 }
  271.                 $links .= $this->_spacesBefore
  272.                         . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  273.             }
  274.  
  275.             if ($this->_expanded && $expansion_after) {
  276.                 $links .= $this->_separator . $this->_spacesAfter;
  277.                 for ($i = $this->_currentPage + $this->_delta +1; $expansion_after; $expansion_after--, $i++) {
  278.                     $print_separator_flag = ($expansion_after != 1);
  279.                     $this->range[$i] = false;
  280.                     $this->_linkData[$this->_urlVar] = $i;
  281.                     $links .= $this->_renderLink($this->_altPage.' '.$i, $i)
  282.                       . $this->_spacesBefore
  283.                       . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  284.                 }
  285.             }
  286.  
  287.         } else {
  288.             //if $this->_totalPages <= (2*Delta+1) show them all
  289.             for ($i=1; $i<=$this->_totalPages; $i++) {
  290.                 if ($i != $this->_currentPage) {
  291.                     $this->range[$i] = false;
  292.                     $this->_linkData[$this->_urlVar] = $i;
  293.                     $links .= $this->_renderLink($this->_altPage.' '.$i, $i);
  294.                 } else {
  295.                     $this->range[$i] = true;
  296.                     $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
  297.                 }
  298.                 $links .= $this->_spacesBefore
  299.                        . (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
  300.             }
  301.         }
  302.         return $links;
  303.     }
  304.  
  305.     // }}}
  306. }
  307. ?>